package com.css.fxfzypg.modules.group.repository.impl;

import com.css.fxfzypg.constants.FxfzConstants;
import com.css.fxfzypg.modules.group.entity.GroupEntity;
import com.css.fxfzypg.modules.group.entity.UserEntity;
import com.css.fxfzypg.modules.group.repository.GroupRepository;
import com.css.fxfzypg.modules.group.vo.GroupVo;
import com.css.fxfzypg.util.PlatformObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Repository
public class GroupRepositoryImpl implements GroupRepository {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void saveGroup(GroupEntity fwGroupEntity) {
        String sql = "insert into ypg_group values(:id , :groupName, :groupLeader, :groupMember, :groupExpert,:delFlag," +
                " :createUser, :createTime, :updateUser, :updateTime, :groupLeaderId, :orgId, :leaderOrgId)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(fwGroupEntity);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public Map<String, Object> getPage(HttpServletRequest request, GroupVo groupVo, int curPage, int pageSize,String allRole,String orgId) {
        StringBuilder sql = new StringBuilder("select * from ypg_group");
        StringBuilder whereSql = new StringBuilder(" where del_flag = '0' ");
        StringBuilder orderSql = new StringBuilder(" order by create_time desc");
        int firstIndex = (curPage - 1) * pageSize;
        int lastIndex = pageSize;
        StringBuilder limitSql = new StringBuilder(" limit " + lastIndex + " offset " + firstIndex);
        if (allRole.contains(FxfzConstants.YPG_PRI_YW_ROLE) || allRole.contains(FxfzConstants.YPG_PRI_GL_ROLE)) {
            whereSql.append(" and org_id like '%" + orgId + "%'");
        }
        if (!PlatformObjectUtils.isEmpty(groupVo.getGroupName())) {
            whereSql.append(" and group_name like '%" + groupVo.getGroupName() + "%'");
        }
        if (!PlatformObjectUtils.isEmpty(groupVo.getGroupLeader())) {
            whereSql.append(" and group_leader like '%" + groupVo.getGroupLeader() + "%'");
        }
        if (!PlatformObjectUtils.isEmpty(groupVo.getGroupMember())) {
            whereSql.append(" and group_member like '%" + groupVo.getGroupMember() + "%'");
        }
        if (!PlatformObjectUtils.isEmpty(groupVo.getGroupExpert())) {
            whereSql.append(" and group_expert like '%" + groupVo.getGroupExpert() + "%'");
        }
        StringBuilder sql1=sql.append(whereSql);
        Integer total = jdbcTemplate.query(sql1.toString(), new BeanPropertyRowMapper<>(GroupEntity.class)).size();
        String sqlWhole = sql1.append(orderSql).append(limitSql).toString();
        List<GroupEntity> GroupEntities = jdbcTemplate.query(sqlWhole, new BeanPropertyRowMapper<>(GroupEntity.class));
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("result", GroupEntities);
        return map;
    }


    @Override
    public void deletGroup(String id, Date updateTime) {
        String sql = "update ypg_group set del_flag= ? ,update_time = ? where id = ?";
        jdbcTemplate.update(sql, '1', updateTime, id);
    }

    @Override
    public GroupEntity selectGroup(String groupId) {
        String sql = "select * from ypg_group where id = ?";
        GroupEntity groupEntity = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<GroupEntity>(GroupEntity.class), groupId);
        return groupEntity;
    }

    @Override
    public void updateGroup(GroupEntity fwGroupEntity) {
        String sql = "update ypg_group set group_name = :groupName, group_leader = :groupLeader, group_member = :groupMember, group_expert = :groupExpert, " +
                "update_user = :updateUser, update_time = :updateTime, group_leader_id = :groupLeaderId, org_id = :orgId, leader_org_id = :leaderOrgId where id= :id";
        SqlParameterSource source = new BeanPropertySqlParameterSource(fwGroupEntity);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public List<GroupEntity> getGroupInfo(String groupLeaderId) {
        /*String sql="select * from ypg_group where group_leader_id = ?";
        GroupEntity groupEntity=new GroupEntity();
        try {
            groupEntity =  jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<GroupEntity>(GroupEntity.class), groupLeaderId);
            return groupEntity;
        } catch (EmptyResultDataAccessException e) {
            return null;
        }*/
        String sql = "select * from ypg_group where group_leader_id = ?";

        List<GroupEntity> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<GroupEntity>(GroupEntity.class), groupLeaderId);
        return list;
    }

    @Override
    public GroupEntity getById(String expertGroupId) {
        try {
            return jdbcTemplate.queryForObject("select * from ypg_group where id = '" + expertGroupId + "'", new BeanPropertyRowMapper<>(GroupEntity.class));
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public void updateSurveyTask(GroupEntity fwGroupEntity) {
        String sql = "update ypg_survey_task set group_name = :groupName, group_leader = :groupLeader, group_leader_id = :groupLeaderId, " +
                "update_user = :updateUser, update_time = :updateTime  where group_id= :id";
        SqlParameterSource source = new BeanPropertySqlParameterSource(fwGroupEntity);
        namedParameterJdbcTemplate.update(sql, source);
    }
}

